<?php

/**
 * YImageResizeBehavior 图片缩放行为
 *
 * @author Hua Yang <htmlcook@gmail.com>
 */
class YImageResizeBehavior extends CActiveRecordBehavior
{
	/**
	 * @var array 图片尺寸
	 */
	private $_sizes = array();

	/**
	 * 获取图片尺寸
	 *
	 * @return array
	 */
	public function getSizes()
	{
		return $this->_sizes;
	}

	/**
	 * 设置图片尺寸
	 *
	 * @param array $sizes
	 */
	public function setSizes($sizes)
	{
		foreach ($sizes as $name => $size) {
			if (empty($size)) {
				$this->deleteSize($name);
				continue;
			}
			$crop = isset($size[2]) ? $size[2] : false;
			$this->setSize($name, $size[0], $size[1], $crop);
		}
	}

	/**
	 * 删除尺寸
	 *
	 * @param array $names
	 */
	public function deleteSize($names)
	{
		foreach ((array)$names as $name) {
			unset($this->_sizes[$name]);
		}
	}

	/**
	 * 设置一个尺寸
	 *
	 * @param string $name 尺寸名
	 * @param integer $weight 宽
	 * @param integer $height 高
	 * @param boolean $crop 是否裁剪
	 */
	public function setSize($name, $weight, $height, $crop = false)
	{
		$this->_sizes[$name] = array($weight, $height, $crop);
	}

	/**
	 * 获取一个尺寸
	 *
	 * @param string $name
	 * @return array|null
	 */
	public function getSize($name)
	{
		if (isset($this->_sizes[$name])) {
			return $this->_sizes[$name];
		}
	}

	/**
	 * 模型保存之前
	 *
	 * @see CActiveRecordBehavior::beforeSave()
	 */
	public function beforeSave($event)
	{
		if (!$event->isValid) {
			return;
		}

		$owner = $this->getOwner();
		if (!$owner->isNewRecord) {
			return;
		}

		if ($owner->isImage()) {
			$sizes = $this->resizeImage($owner->getPath(), $this->_sizes);

			if (is_array($sizes)) {
				$owner->meta['sizes'] = $sizes;
			}
		}
	}

	/**
	 * 缩放图片
	 *
	 * @param string $filePath 文件路径
	 * @param array $sizes 图片尺寸
	 * @return boolean|array
	 */
	public function resizeImage($filePath, $sizes)
	{
		if (!Yii::app()->hasComponent('image')) {
			return false;
		}

		/** @var File $owner */
		$owner = $this->getOwner();
		$metaSizes = array();

		$parts = pathinfo($filePath);
		$filename = $parts['filename'];
		$ext = $parts['extension'];

		/** @var YGDImage $image */
		$image = Yii::app()->image->load($filePath);
		list($sWidth, $sHeight) = $image->getSize();

		$baseUri = str_replace(basename($owner->uri), '', $owner->uri);
		if ('/' !== substr($baseUri, -1)) {
			$baseUri .= '/';
		}

		foreach ($sizes as $name => $size) {
			if (!isset($size[2])) {
				$size[2] = false;
			}

			list($width, $height, $crop) = $size;

			if ($sWidth < $width && $sHeight < $height
				&& ($name !== File::IMAGE_THUMBNAIL || $name !== File::IMAGE_ORIGIN)) {
				continue;
			}

			if (!$crop) {
				$thumbnail = $image->thumbnail($width, $height);
			} else {
				$thumbnail = $image->thumbnail($width, $height, YImageInterface::THUMBNAIL_OUTBOUND);
			}

			$imageSize = $thumbnail->getSize();

			if ($name === File::IMAGE_ORIGIN) {
				$saveFilePath = $owner->getPath();
				$uri = $owner->uri;
			} else {
				$uri = $baseUri . "{$filename}_{$imageSize[0]}x{$imageSize[1]}.{$ext}";
				$saveFilePath = $owner->localPath($uri);
			}

			$thumbnail->save($saveFilePath);
			$metaSizes[$name] = array(
				'uri' => $uri,
				'width' => $imageSize[0],
				'height' => $imageSize[1],
			);
		}

		if (!isset($metaSizes[File::IMAGE_ORIGIN])) {
			$imageSize = $image->getSize();
			$metaSizes[File::IMAGE_ORIGIN] = array(
				'uri' => $owner->uri,
				'width' => $imageSize[0],
				'height' => $imageSize[1],
			);
		}

		return $metaSizes;
	}
}